<?php

defined('_JEXEC') or die('Restricted access');
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');

class ConsoleModelTestPhones extends JModelList
{
    /**store
     * @param $inputs
     * @return bool
     */
    public function store($inputs) {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $user = JFactory::getUser();
        $query->insert('pp_whitelist')
            ->columns(
                array(
                    $db->quoteName('account_id'),
                    $db->quoteName('imei'),
                    $db->quoteName('device_id')
                )
            )
            ->values(' "'.$user->id.'",  "'.$inputs['imei'].'",  "'.$inputs['device_id'].'" ');

        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
        return true;
    }

    /**update
     * @param $inputs
     * @return bool
     */
    public function update($inputs) {
        $trackDate = JFactory::getDate()->format('Y-m-d H:i:s');
        $id = $inputs['id'];
        $db = $this->getDbo();
        $query = $db->getQuery(true)
            ->update('pp_whitelist')
            ->set("imei = '".trim($inputs['imei'])."'")
            ->set("device_id = '".trim($inputs['device_id'])."'")
            ->where('id = ' . (int) $id);

        // Set the query and execute the update.
        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }

        return true;
    }

    protected function getListQuery()
    {
        $app = JFactory::getApplication('site');
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $user = JFactory::getUser();
        $params = $app->getParams();
        $this->setState('params', $params);
        $query->select(
            '*'
        )
            ->from('pp_whitelist')->where("account_id = ".$user->id);
        $query->order('id desc');

        return $query;
    }



    public function getItem($id = null)
    {

        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('*')
            ->from('pp_whitelist')->where('id='.$id);
        $db->setQuery($query);
        $Item= $db->loadObject();
        return $Item;
    }

    public function selectAll($id = null) {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('*')->from('pp_whitelist')->where('account_id=' . $id)->order('id');
        $db->setQuery($query);
        $items = $db->loadObjectList();

        return $items;
    }

    /**delete
     * @param $id
     */
    public function delete($id)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->delete('pp_whitelist')

            ->where('id='.$id);

        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
        return true;
    }

    public function getCount()
    {
        $user = JFactory::getUser();
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('count(*)')
            ->from('pp_whitelist')->where("account_id = ".$user->id);
        $db->setQuery($query);
        $Item= $db->loadResult();
        return $Item;
    }

}
